package com.vitaf.cash.app.ui.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import com.vitaf.cash.app.R
import com.vitaf.cash.app.common.VitafComDlog
import com.vitaf.cash.app.databinding.VitafDlogUpdateBinding
import com.vitaf.cash.app.databinding.VitafDlogUpdateReasonItemBinding
import com.vitaf.cash.app.event.VitafEventPointType
import com.vitaf.cash.app.utils.helper.VitafConfigsHp

/**
 * GombeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/13 15:41
 * @Annotation：提示dialog
 */
class VitafUpdateDlog: VitafComDlog {

    private lateinit var binding: VitafDlogUpdateBinding
    private var mCallback: IDialogCallback? = null

    override val pageName: String = "Update_Version"

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context, remark: String) : super(context) {
        pageRemark = remark
    }
    constructor(context: Context, remark: String, callback: IDialogCallback?) : super(context) {
        mCallback = callback
        pageRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun createVitafContentView(): View {
        binding = VitafDlogUpdateBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    @SuppressLint("SetTextI18n")
    override fun initializeVitafViews() {
        val releaseData = VitafConfigsHp.takeGombeCreditVersionObject()
        releaseData?.let {
            binding.textVersionName.text = "V${it.optString("versionName")}"
            //添加更新描述
            addGombeCreditUpdateReason(it.optString("versionRemark"))

            //是否强制更新
            val isMustUpdate = VitafConfigsHp.isGombeCreditUpdateMust()
            if (isMustUpdate){
                binding.buttonUpdateWait.visibility = View.GONE
                binding.imageClose.visibility = View.GONE
                setCancelable(false)
                setCanceledOnTouchOutside(false)
            } else {
                binding.buttonUpdateWait.visibility = View.VISIBLE
                binding.imageClose.visibility = View.VISIBLE
                setCancelable(true)
                setCanceledOnTouchOutside(true)
            }
        }
    }

    override fun setupVitafEventListeners() {
        setOnClickListener(binding.imageClose) //关闭
        setOnClickListener(binding.buttonUpdate) //立即更新
        setOnClickListener(binding.buttonUpdateWait) //稍后更新
    }

    override fun loadVitafData() {
    }

    override fun onVitafEventViewClick(v: View?) {
        when(v?.id){
            R.id.imageClose -> {
                addEventPointRequestAction(VitafEventPointType.GOMBECREDIT_TRACK_CLICK.UPDATE_VERSION_CLOSE,pageRemark)
            }
            R.id.buttonUpdate -> {
                addEventPointRequestAction(VitafEventPointType.GOMBECREDIT_TRACK_CLICK.UPDATE_VERSION_NOW,pageRemark)
                mCallback?.onUpdateClick()
            }
            R.id.buttonUpdateWait -> {
                addEventPointRequestAction(VitafEventPointType.GOMBECREDIT_TRACK_CLICK.UPDATE_VERSION_WITHIN,pageRemark)
            }
        }
        dismiss()
    }

    /**
     * 添加更新描述
     */
    private fun addGombeCreditUpdateReason(remark: String){
        binding.updateReasonLayout.removeAllViews()
        if (!remark.contains("$")){
            val itemBinding = VitafDlogUpdateReasonItemBinding.inflate(layoutInflater)
            itemBinding.textReason.text = remark
            binding.updateReasonLayout.addView(itemBinding.root)
            return
        }
        val list = remark.split("$")
        list.forEach {
            val itemBinding = VitafDlogUpdateReasonItemBinding.inflate(layoutInflater)
            itemBinding.textReason.text = it
            binding.updateReasonLayout.addView(itemBinding.root)
        }
    }

    interface IDialogCallback {
        //立即更新
        fun onUpdateClick()
    }
}